import 'package:flutter/material.dart';
//上拉刷新，下拉加载
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:Smart/res/colors_res.dart';
import 'package:Smart/utils/routers/page_jump_service.dart';

class RefreshPage extends StatelessWidget {
  const RefreshPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          leading: GestureDetector(
            onTap: () {
              PageJumpService.instance().goBack();
            },
            child: Icon(
              Icons.arrow_back_ios,
              size: 20,
              color: Color(0XFF231815),
            ),
          ),
          backgroundColor: ColorsRes.color_ffffff,
          title: Text(
            '刷新插件',
            style: TextStyle(color: ColorsRes.color_000000),
          ),
        ),
      body: RefreshExample(),
    );
  }
}

class RefreshExample extends StatefulWidget {
  RefreshExample({Key key}) : super(key: key);

  @override
  _RefreshExampleState createState() => _RefreshExampleState();
}

class _RefreshExampleState extends State<RefreshExample> {
// 条目总数
  int _count = 40;
  EasyRefreshController _controller;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }


  @override
  Widget build(BuildContext context) {
    return Container(
       child: EasyRefresh.custom(
        enableControlFinishRefresh: false,
        enableControlFinishLoad: true,
        controller: _controller,
        header: ClassicalHeader(),
        footer: ClassicalFooter(),
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2), () {
            print('上拉刷新');
            setState(() {
              _count = 20;
            });
            _controller.resetLoadState();
          });
        },
        onLoad: () async {
          await Future.delayed(Duration(seconds: 2), () {
            print('下拉加载');
            setState(() {
              _count += 10;
            });
            _controller.finishLoad(noMore: _count >= 40);
          });
        },
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (context, index) {
                return Container(
                  width: 60.0,
                  height: 60.0,
                  child: Center(
                    child: Text('$index'),
                  ),
                  color: index % 2 == 0 ? Colors.grey[300] : Colors.transparent,
                );
              },
              childCount: _count,
            ),
          ),
        ],
      ),
    );
  }
}

